import 'package:froom_annotation/froom_annotation.dart';
import 'package:get/get.dart';
import "../../lib/src/type_converter.dart";
// ignore: uri_has_not_been_generated
part 'user.g.dart';

/// 用户实体 - 测试用
@Entity(tableName: 'users')
class User {
  @PrimaryKey(autoGenerate: true)
  final int? id;

  final String name;

  @ColumnInfo(name: 'email')
  final String email;

  final int age;

  final String status; // active, inactive, deleted

  @ColumnInfo(name: 'created_at')
  final DateTime? createdAt;

  @ColumnInfo(name: 'updated_at')
  final DateTime? updatedAt;

  User(
      {this.id,
      required this.name,
      required this.email,
      required this.age,
      this.status = 'active',
      this.createdAt,
      this.updatedAt});

  User copyWith({
    int? id,
    String? name,
    String? email,
    int? age,
    String? status,
    DateTime? createdAt,
    DateTime? updatedAt,
  }) {
    return User(
      id: id ?? this.id,
      name: name ?? this.name,
      email: email ?? this.email,
      age: age ?? this.age,
      status: status ?? this.status,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
    );
  }

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is User &&
          runtimeType == other.runtimeType &&
          id == other.id &&
          name == other.name &&
          email == other.email &&
          age == other.age &&
          status == other.status;

  @override
  int get hashCode =>
      id.hashCode ^
      name.hashCode ^
      email.hashCode ^
      age.hashCode ^
      status.hashCode;

  @override
  String toString() {
    return 'User{id: $id, name: $name, email: $email, age: $age, status: $status, createdAt: $createdAt, updatedAt: $updatedAt}';
  }

  static String getTableName() => 'users';
  static List<String> getPrimaryKey() => ['id'];
  static Map<String, String> getFieldCol() => {
        'id': 'id',
        'name': 'name',
        'email': 'email',
        'age': 'age',
        'status': 'status',
        'createdAt': 'created_at',
        'updatedAt': 'updated_at',
      };
  static User toEntity(Map<String, Object?> map) => User(
        id: map['id'] as int?,
        name: map['name'] as String,
        email: map['email'] as String,
        age: map['age'] as int,
        status: map['status'] as String,
        createdAt: map['created_at'] != null
            ? DateTime.parse(map['created_at'] as String)
            : null,
        updatedAt: map['updated_at'] != null
            ? DateTime.parse(map['updated_at'] as String)
            : null,
      );
  static Map<String, Object?> toMap(User user) => {
        'id': user.id,
        'name': user.name,
        'email': user.email,
        'age': user.age,
        'status': user.status,
        'created_at': user.createdAt?.toIso8601String(),
        'updated_at': user.updatedAt?.toIso8601String(),
      };
}
